package com.kitchen.ui.box9grid
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class AbstractBox9grid extends Sprite
	{
		protected var bitdata:BitmapData;
		protected var w:Number;
		protected var h:Number;
		
		
		public function AbstractBox9grid(bitdata:BitmapData, w:Number, h:Number)
		{
			super();
			this.bitdata = bitdata;
			this.h = h;
			this.w = w;
			
		}
		
		protected function setCData(cdata:Object):void{
			//top
			var tldata:BitmapData = new BitmapData(cdata.tl.w, cdata.tl.h);
			tldata.copyPixels(this.bitdata, new Rectangle(cdata.tl.x, cdata.tl.y, cdata.tl.w, cdata.tl.h), new Point(0, 0));
			var tlmap:Bitmap = new Bitmap(tldata);
			var tcdata:BitmapData = new BitmapData(cdata.tc.w, cdata.tc.h);
			tcdata.copyPixels(this.bitdata, new Rectangle(cdata.tc.x, cdata.tc.y, cdata.tc.w, cdata.tc.h), new Point(0, 0));
			var tcshape:Shape = new Shape();
			tcshape.graphics.lineStyle(0, 0xffffff, 0.001);
			tcshape.graphics.beginBitmapFill(tcdata);
			tcshape.graphics.drawRect(0, 0, this.w-cdata.tl.w-cdata.tr.w, cdata.tc.h);
			tcshape.graphics.endFill();
			var trdata:BitmapData = new BitmapData(cdata.tr.w, cdata.tr.h);
			trdata.copyPixels(this.bitdata, new Rectangle(cdata.tr.x, cdata.tr.y, cdata.tr.w, cdata.tr.h), new Point(0, 0));
			var trmap:Bitmap = new Bitmap(trdata);
			tlmap.y = tcshape.y = trmap.y = 0;
			trmap.x = this.w - cdata.tr.w;
			tcshape.x = cdata.tl.w;
			this.addChild(tlmap);
			this.addChild(tcshape);
			this.addChild(trmap);
			
			//center
			var ch:Number = this.h - cdata.tl.h - cdata.bl.h;
			var cldata:BitmapData = new BitmapData(cdata.cl.w, cdata.cl.h);
			cldata.copyPixels(this.bitdata, new Rectangle(cdata.cl.x, cdata.cl.y, cdata.cl.w, cdata.cl.h), new Point(0, 0));
			var clshape:Shape = new Shape();
			clshape.graphics.lineStyle(0, 0xffffff, 0.001);
			clshape.graphics.beginBitmapFill(cldata);
			clshape.graphics.drawRect(0, 0, cdata.cl.w, ch);
			clshape.graphics.endFill();
			var ccdata:BitmapData = new BitmapData(cdata.cc.w, cdata.cc.h);
			ccdata.copyPixels(this.bitdata, new Rectangle(cdata.cc.x, cdata.cc.y, cdata.cc.w, cdata.cc.h), new Point(0, 0));
			var ccshape:Shape = new Shape();
			ccshape.graphics.lineStyle(0, 0xffffff, 0.001);
			ccshape.graphics.beginBitmapFill(ccdata);
			ccshape.graphics.drawRect(0, 0, this.w-cdata.cl.w-cdata.cr.w, ch);
			ccshape.graphics.endFill();
			var crdata:BitmapData = new BitmapData(cdata.cr.w, cdata.cr.h);
			crdata.copyPixels(this.bitdata, new Rectangle(cdata.cr.x, cdata.cr.y, cdata.cr.w, cdata.cr.h), new Point(0, 0));
			var crshape:Shape = new Shape();
			crshape.graphics.lineStyle(0, 0xffffff, 0.001);
			crshape.graphics.beginBitmapFill(crdata);
			crshape.graphics.drawRect(0, 0, cdata.cr.w, ch);
			crshape.graphics.endFill();
			clshape.y = ccshape.y = crshape.y = cdata.tl.h;
			clshape.x = 0;
			ccshape.x = cdata.cl.w;
			crshape.x = this.w - cdata.cr.w;
			this.addChild(clshape);
			this.addChild(ccshape);
			this.addChild(crshape);
			//bottom
			var bldata:BitmapData = new BitmapData(cdata.bl.w, cdata.bl.h);
			bldata.copyPixels(this.bitdata, new Rectangle(cdata.bl.x, cdata.bl.y, cdata.bl.w, cdata.bl.h), new Point(0, 0));
			var blshape:Shape = new Shape();
			blshape.graphics.lineStyle(0, 0xffffff, 0.001);
			blshape.graphics.beginBitmapFill(bldata);
			blshape.graphics.drawRect(0, 0, cdata.bl.w, cdata.bl.h);
			blshape.graphics.endFill();
			var bcdata:BitmapData = new BitmapData(cdata.bc.w, cdata.bc.h);
			bcdata.copyPixels(this.bitdata, new Rectangle(cdata.bc.x, cdata.bc.y, cdata.bc.w, cdata.bc.h), new Point(0, 0));
			var bcshape:Shape = new Shape();
			bcshape.graphics.lineStyle(0, 0xffffff, 0.001);
			bcshape.graphics.beginBitmapFill(bcdata);
			bcshape.graphics.drawRect(0, 0, this.w-cdata.bl.w-cdata.br.w, cdata.bl.h);
			bcshape.graphics.endFill();
			var brdata:BitmapData = new BitmapData(cdata.br.w, cdata.br.h);
			brdata.copyPixels(this.bitdata, new Rectangle(cdata.br.x, cdata.br.y, cdata.br.w, cdata.br.h), new Point(0, 0));
			var brshape:Shape = new Shape();
			brshape.graphics.lineStyle(0, 0xffffff, 0.001);
			brshape.graphics.beginBitmapFill(brdata);
			brshape.graphics.drawRect(0, 0, cdata.br.w, cdata.br.h);
			brshape.graphics.endFill();
			blshape.y = bcshape.y = brshape.y = this.h-cdata.bl.h;
			blshape.x = 0;
			bcshape.x = cdata.bl.w;
			brshape.x = this.w - cdata.br.w;
			this.addChild(blshape);
			this.addChild(bcshape);
			this.addChild(brshape);
			/*
			@todo
			this.graphics.lineStyle(1, 0xff8800, 1);
			this.graphics.beginBitmapFill(tldata);
			this.graphics.drawRect(0, 0, cdata.tl.w, cdata.tl.h);
			this.graphics.beginBitmapFill(tcdata);
			this.graphics.drawRect(cdata.tl.w, 0, this.w-cdata.tl.w-cdata.tr.w, cdata.tc.h);
			this.graphics.beginBitmapFill(trdata);
			this.graphics.drawRect(this.w-cdata.tr.w, 0, cdata.tr.w, cdata.tr.h);
			
			var ch:int = this.h - cdata.tl.h - cdata.bl.h;
			this.graphics.beginBitmapFill(cldata);
			this.graphics.drawRect(0, cdata.tl.h, cdata.cl.w, ch);
			this.graphics.beginBitmapFill(ccdata);
			this.graphics.drawRect(cdata.cl.w, cdata.tl.h, this.w-cdata.cl.w-cdata.cr.w, ch);
			this.graphics.beginBitmapFill(crdata);
			this.graphics.drawRect(this.w-cdata.cr.w, cdata.tl.h, cdata.cr.w, ch);
			var by:int = this.h - cdata.bl.h;
			this.graphics.beginBitmapFill(bldata);
			this.graphics.drawRect(0, by, cdata.bl.w, cdata.bl.h);
			this.graphics.beginBitmapFill(bcdata);
			this.graphics.drawRect(cdata.bl.w, by, this.w-cdata.bl.w-cdata.br.w, cdata.bc.h);
			this.graphics.beginBitmapFill(brdata);
			this.graphics.drawRect(this.w-cdata.br.w, by, cdata.br.w, cdata.br.h);
			this.graphics.endFill();
			*/
		}
		
	}
}